package xyz.fuqufyb.aec.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.fuqufyb.aec.data.JsonResult;
import xyz.fuqufyb.aec.data.TableResult;
import xyz.fuqufyb.aec.entity.AdminUser;
import xyz.fuqufyb.aec.entity.Menu;
import xyz.fuqufyb.aec.service.IMenuService;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 用户: fyb
 * @description class: MenuController
 * @date 时间: 2021/1/29 15:01
 */

@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    /**
     * 左侧菜单栏数据
     * @param session
     * @return
     */
    @RequestMapping("/side")
    public JsonResult getMenuTree(HttpSession session) {

        AdminUser user = (AdminUser) session.getAttribute("user");
        if (user != null) {
            List<Menu> menuTree = menuService.getMenuTree(user.getId());

            System.out.println(menuTree);
            return (JsonResult) JsonResult.getJsonSuccess(menuTree).put("username", user.getEmail());
        }
        return JsonResult.getJsonError("您还没登录");
    }

    /**
     * 分页查询
     * @param maps
     * @return
     */
    @GetMapping("/list")
    public TableResult list(@RequestParam Map<String, Object> maps) {

        TableResult tableResult = menuService.getMenuList(maps);

        if (tableResult != null) {
            return tableResult;
        }
        return TableResult.getTableResultError();
    }

    /**
     * 全查，数据，数据给树形结构
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/tree")
    public String tree() throws JsonProcessingException {
        List<Menu> menuTree = menuService.getMenuTree();
        if (menuTree != null) {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(menuTree);
        }
        return null;
    }

    /**
     * id查询  菜单数据
     * @param id
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/info")
    public String info(@RequestParam("id") Integer id) throws JsonProcessingException {

        Menu menu = menuService.selectByPrimaryKey(id);
        if (menu != null) {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(menu);
        }
        return null;
    }

    /**
     * 新增 菜单数据
     * @param menu
     * @return
     */
    @PostMapping("/add")
    public JsonResult addMenu(Menu menu) {
        int i = menuService.insertSelective(menu);
        if (i > 0) {
            return JsonResult.getJsonSuccess("新增成功");
        }
        return JsonResult.getJsonError();
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @PostMapping("/update")
    public JsonResult updateMenu(Menu menu) {
        int i = menuService.updateByPrimaryKeySelective(menu);
        if (i > 0) {
            return JsonResult.getJsonSuccess("修改成功");
        }

        return JsonResult.getJsonError();
    }
    @PostMapping("/delete")
    public JsonResult deleteMenuIds(@RequestParam("ids") Integer[] ids){

        int i  = menuService.deleteMenuIds(Arrays.asList(ids));

        if (i>0){
            return JsonResult.getJsonSuccess("删除成功");
        }
        return JsonResult.getJsonError();
    }

}
